<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
	</head>
	<body>
		<form class="mui-input-group">
			<div class="mui-input-row">
				<label>计划单</label>
				<div style="height:100%;display:flex;flex-direction: row;align-items: center;">
					<input type="text" id="planCode" class="mui-input-clear">
					<button id="scanBtn">扫描</button>
				</div>

			</div>
			<div class="mui-input-row">
				<label>工序名称</label>
				<select id="gx" name="gx">
				</select>
			</div>
			<div class="mui-input-row">
				<label>子工序</label>
				<select id="subgx" name="subgx">
				</select>
			</div>
			<div class="mui-input-row">
				<label>数量</label>
				<input type="text" id="num" name="num" class="mui-input-clear">
			</div>
			<div class="mui-button-row">
				<button type="button" class="mui-btn mui-btn-primary" onclick="save()">保存</button>
			</div>
		</form>
	</body>
</html>
<script src="js/mui.min.js"></script>
<script src="js/mui.meeting.js"></script>
<script>
	var barcode;
	var users;

	mui.plusReady(function() {
		//当前登录用户
		users = JSON.parse(localStorage.getItem('$users') || '[]');

		//扫描获取计划单号
		document.getElementById("scanBtn").addEventListener("tap", function() {
			startScan();
		})
		//计划单号改变触发事件
		document.getElementById('planCode').addEventListener('change', function() {
			var plancode = document.getElementById('planCode').value;
			loadgx(plancode);
		})
		//工序改变触发事件
		document.getElementById("gx").addEventListener("change", function() {
			var gxid = document.getElementById("gx").value;
			loadsubgx(gxid);
		})
	})

	//打开二维码扫描界面
	function startScan() {
		meeting.Form.openWindow("scan", "扫描二维码", {
			backId: "edit"
		});
	}

	// 扫码成功回调
	window.addEventListener('scaned', function(event) {
		console.log("扫码成功：" + event.detail.qrcode);
		document.getElementById('planCode').value = event.detail.qrcode;
		loadgx(event.detail.qrcode);
	});

	//加载工序
	function loadgx(planCode) {
		meeting.Get(meeting.Url.getDefaultSiteUrl("gx/find_plancode"), {
			data: {
				plan_code: planCode
			},
			success: function(data) {
				if (data.state > 0) {
					document.getElementById("gx").innerHTML = "";
					for (var i = 0; i < data.data.length; i++) {
						var item = data.data[i];
						var op = new Option();
						op.value = item.onlyC;
						op.text = item.name;
						document.getElementById("gx").appendChild(op);
						if (i == 0) {
							loadsubgx(item.onlyC);
						}
					}

				} else {
					mui.toast("获取失败")
				}
			}
		})
	}

	//加载子工序
	function loadsubgx(gxid) {
		meeting.Get(meeting.Url.getDefaultSiteUrl("subgx/find_gxid"), {
			data: {
				gxid: gxid
			},
			success: function(data) {
				if (data.state > 0) {
					document.getElementById("subgx").innerHTML = "";
					for (var i = 0; i < data.data.length; i++) {
						var item = data.data[i];
						var op = new Option();
						op.value = item.onlyC;
						op.text = item.name;
						document.getElementById("subgx").appendChild(op);
					}

				} else {
					mui.toast("获取失败")
				}
			}
		})
	}

	//保存
	function save() {
		meeting.Post(meeting.Url.getDefaultSiteUrl("order/add"), {
			data: {
				userid: users.onlyC,
				gxid: document.getElementById("gx").value,
				subgxid: document.getElementById("subgx").value,
				num: document.getElementById("num").value
			},
			success: function(data) {
				if (data.state > 0) {
					mui.toast("保存成功")
				} else {
					mui.toast("保存失败")
				}
			}
		})
	}
</script>
